import { observable, action, computed } from 'mobx';
import { message } from 'antd';
import { createContext } from 'react';
import dayjs from 'dayjs';

import request from '@/services/newRequest';
import { useHistory } from 'react-router-dom';

class CompanySetStore {
  @observable tableData = [
     {
      "id": "1",
      "methodName": "com.stock.qfii.commisionmgr.HolidayPool:getInstance",
      "isdiff": "是",
     },

     {
      "id": "2",
      "methodName": "com.stock.qfii.commisionmgr.HolidayPool:refresh",
      "isdiff": "是",
     },

     {
      "id": "3",
      "methodName": "com.stock.qfii.commisionmgr.CommisionChargeDef:getNextReceiveDate",
      "isdiff": "是",
     },

  ];

  @observable options = [
    {
      value: 'v1',
      label: 'qfii-v1',
    },
    {
      value: 'v2',
      label: 'qfii-v2',
    },
  ];

  @observable loading = false;

  @observable statusLoading = false;

  @observable newModalVisible = false;

  @observable newLoading = false;

  @observable modalType = 'new';

  @observable modalData = {};

  @observable rowData = {};


  @observable pagination = {
    size: 'small',
    pageSize: 10,
    currentPage: 1,
    total: 0,
    showSizeChanger: true,
    onChange: (currentP, size) => {
      this.qryTableDate(currentP, size);
    },
    onShowSizeChange: (currentP, size) => {
      this.qryTableDate(currentP, size);
    },
    showTotal: (totalP) => `共 ${totalP} 条记录`,
  };

  @observable searchParams = {
    name: undefined,
    gmtBegin: dayjs(new Date()).subtract(7, 'days').format('YYYY-MM-DD'),
    gmtEnd: dayjs(new Date()).format('YYYY-MM-DD'),
  };

  @computed get modalTitle() {
    let res = '项目';
    if (this.modalType === 'new') {
      res = `新增接口`;
    }
    return res;
  }

  @action.bound setData(data = {}) {
    Object.entries(data).forEach((item) => {
      this[item[0]] = item[1];
    });
  }

  @action.bound openModal(type, record = {}) {
    this.modalType = type;
    this.newModalVisible = true;
    this.modalData = {
      name: record.interfacename,
    };
  }

  // 列表数据
  @action.bound
  async qryTableDate(page = 1, size = this.pagination.pageSize) {
    //查询列表数据
    this.loading = true;
    const res = await request({
      url: '/interfaceMethod/pageList',
      method: 'post',
      data: { page, size, ...this.searchParams },
    });

    if (res.success) {
      const resData = res;
      this.tableData = resData.pageList || [];
      this.pagination.total = resData.totalCount;
      this.pagination.currentPage = page;
      this.pagination.pageSize = size;
    }
  }

  // 查询历史版本数据
  @action.bound
  async qryAllVersion(page = 1, size = this.pagination.pageSize) {
    //查询列表数据
    this.loading = true;
    //查询历史版本数据
    const resVerion = await request({
      url: '/projectversion/allVersion',
      method: 'post',
      data: { page, size, ...this.searchParams },
    });

    if (resVerion.success) {
      const resVerionData = resVerion;
      this.options = resVerionData.pageList || [];
    }

    this.loading = false;

  }

  // 新建厂商
  @action.bound
  async addNew(data) {
    this.newLoading = true;
    const res = await request({
      url: '/interfaceMethod/insert',
      method: 'post',
      data,
    });
    if (res.success) {
      message.success('新建成功！');
      this.newModalVisible = false;
      this.qryTableDate();
    }
    this.newLoading = false;
  }

  // 删除
  @action.bound
  async delOne(data) {
    this.recordLoding = true;
    const res = await request({
      url: '/interfaceMethod/delete',
      method: 'post',
      data: { no: data },
    });
    if (res.success) {
      message.success('删除成功！');
      this.qryTableDate();
    }
    this.recordLoding = false;
  }

  // 删除
  @action.bound
  async excute(data) {
    this.recordLoding = true;
    const res = await request({
      url: '/hisCode/allVersion',
      method: 'post',
      data: { methodName: data },
    });
    if (res.success) {
      message.success('删除成功！');
      this.qryTableDate();
    }
    this.recordLoding = false;
  }

  // 新建对比文件列表
  @action.bound
  async opendiffList(data) {
    this.newLoading = true;
    const res = await request({
      url: '/hisCode/allVersion',
      method: 'post',
      data,
    });
    if (res.success) {
      message.success('新建成功！');
      this.newModalVisible = false;
      this.qryTableDate();
    }
    this.newLoading = false;
  }  

  // 查询历史版本列表
  @action.bound
  async hisVesionList(data) {
    this.newLoading = true;
    const res = await request({
      url: '/hisCode/allVersion',
      method: 'post',
      data,
    });
    if (res.success) {
      message.success('新建成功！');
      this.newModalVisible = false;
      this.qryTableDate();
    }
    this.newLoading = false;
  } 
  

}

export default createContext(new CompanySetStore());
